function targetCvgBatch {
    echo -e "\033[1mSubmitting targetCvgBatch Jobs "
    mkdir -p $STAT_DIR

    if [[ -n $1 ]];then
    	export GATK_THREADS=$1
    else
	    export GATK_THREADS=$MAX_CORE_NUM
	    echo "\$MAX_CORE_NUM $MAX_CORE_NUM will be used to set GATK thread number. To change it: -t \"variantCallBatch num\". e.g. -t \"variantCallBatch 12\""
    fi

    echo -n "$GATK_THREADS GATK threads h_vmem=7G used"
    echo -e " \033[0m"

	$SUB -N cvgTbatch$FC \
	      $SUBARGS -l h_vmem=7G -pe $PE $GATK_THREADS -v GATK_THREADS,BAM_DIR,STAT_DIR \
	      "$CMD_DIR/depthOfCoverageBatch.sh" "$STAT_DIR/${FC}_target_cvg" $TARGET
}

function genomeCvgBatch {
    echo -e "\033[1mSubmitting targetCvgBatch Jobs \033[0m"
    mkdir -p $STAT_DIR
    export GATK_THREADS=$MAX_CORE_NUM

	$SUB -N cvgGbatch$FC \
	      $SUBARGS -l h_vmem=3G -pe $PE $GATK_THREADS -v GATK_THREADS,BAM_DIR,STAT_DIR \
	      "$CMD_DIR/depthOfCoverageBatch.sh" "$STAT_DIR/${FC}_genome_cvg"
}


function variantCallBatch {
    echo -e "\033[1mSubmitting variantCallBatch Jobs "
    mkdir -p $VCF_DIR

    if [[ -n $1 ]];then
    	export GATK_THREADS=$1
    else
	    export GATK_THREADS=$MAX_CORE_NUM
	    echo "\$MAX_CORE_NUM $MAX_CORE_NUM will be used to set GATK thread number. To change it: -t \"variantCallBatch num\". e.g. -t \"variantCallBatch 12\""
    fi

    vmem=$((6 + $GATK_THREADS))
    echo "${vmem}G"

    echo -n "$GATK_THREADS GATK threads h_vmem=${vmem}G used"
    echo -e " \033[0m"

    $SUB -N varcalBatch$FC \
	     $SUBARGS -pe $PE $GATK_THREADS -v GATK_THREADS,TARGET,BAM_DIR,VCF_DIR -l h_stack=1024m,h_vmem="${vmem}G" \
	     "$CMD_DIR/variantCalBatch.sh" "$VCF_DIR/${FC}_mbq_${mbq}_mmq_${mmq}" ${mbq} ${mmq}

# h_vmem for total job memory
# 6G (overhead) + (0.8G)* slots (i.e. GATK threads)
#parallel environment:  DJ range: 12
#usage    1:                 cpu=19:23:54:40, mem=14986058.28825 GBs, io=983.17376, vmem=8.775G, maxvmem=8.775G

    $SUB -hold_jid varcalBatch$FC -N snpFiltr$FC \
	     $SUBARGS -l h_stack=512m,h_vmem=7G \
	     "$CMD_DIR/variantFiltrationNoMask.sh" "$VCF_DIR/${FC}_mbq_${mbq}_mmq_${mmq}"

    $SUB -hold_jid snpFiltr${FC} -N snpEff${FC}  \
             $SUBARGS -l h_stack=512m,h_vmem=6.5G \
             "$CMD_DIR/snpEff.sh" "$VCF_DIR/${FC}_mbq_${mbq}_mmq_${mmq}_filtered_nomask" $SNPEFF

}

function target_coverages {
    echo -e "\033[1mSubmitting target_coverages Jobs \033[0m"
    mkdir -p $STAT_DIR
    HOLDJID="recalTableFULL${LINE}"
    INFILE="s_${LINE}_recal.FULL.bam"

   if [ -z "$MAX_CORE_NUM" ]
   	then 
   		export GATK_THREADS=4   	
   	else
   		export GATK_THREADS=$MAX_CORE_NUM
		fi
#     local JOB_MEM1=$(adjustWorkingMem 256M $GATK_THREADS)
#     local JOB_MEM2=$(adjustWorkingMem 8G $GATK_THREADS)

	$SUB -hold_jid $HOLDJID -N cvgUqT$LINE \
	      $SUBARGS -l h_stack=256M,h_vmem=8G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_recal_target${2}" "$INFILE" $TARGET

# 	$SUB -hold_jid $HOLDJID -N cvgNdT$LINE \
# 	      $SUBARGS -l h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
# 	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_nodup_target${2}" "s_${LINE}_nodup.bam" $1
#
# 	$SUB -hold_jid $HOLDJID -N cvgT$LINE \
# 	      $SUBARGS -l h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
# 	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_target${2}" "s_${LINE}.bam" $1
 }

function flank_coverage {
    echo -e "\033[1mSubmitting flank_coverage Jobs \033[0m"
    mkdir -p $STAT_DIR
    HOLDJID="recalTableFULL${LINE}"
    export GATK_THREADS=4
    if [ -z $SNPDB ];then
      INFILE="s_${LINE}_nodup_uniq.bam"
      HOLDJID="dedupNIndexBam${LINE}"
    else
      INFILE="s_${LINE}_nodup_uniq_recal.FULL.bam"
    fi
	$SUB -hold_jid $HOLDJID -N cvgUqF$LINE \
	      $SUBARGS -l h_vmem=2.1G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_nodup_uniq_recal_flank${2}" "$INFILE" $1
 }

function genome_coverage_merge {
    echo -e "\033[1mSubmitting genome_coverage_merge Jobs \033[0m"
    mkdir -p $STAT_DIR
    local HOLDJID="mergeChrTable_realn${LINE}"
   if [ -z "$MAX_CORE_NUM" ]
   	then 
   		export GATK_THREADS=4   	
   	else
   		export GATK_THREADS=$MAX_CORE_NUM
		fi

#     local JOB_MEM1=$(adjustWorkingMem 256M $GATK_THREADS)
#     local JOB_MEM2=$(adjustWorkingMem 7G $GATK_THREADS)

	$SUB -hold_jid $HOLDJID -N cvgUqG$LINE \
	      $SUBARGS -l h_stack=256M,h_vmem=8G \
               -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_nodup_uniq_recal_merge" "s_${LINE}_merged_nodup_uniq_realn_recal.bam"
}
function genome_coverage {
    echo -e "\033[1mSubmitting genome_coverage Jobs \033[0m"
    mkdir -p $STAT_DIR
    export GATK_THREADS=4
    HOLDJID="recalTableFULL${LINE}"
    INFILE="s_${LINE}_recal.FULL.bam"

	$SUB -hold_jid $HOLDJID -N cvgUqG$LINE \
	      $SUBARGS -l h_stack=256M,h_vmem=8G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_recal_genome" "$INFILE"

#  	$SUB -hold_jid $HOLDJID -N cvgNdG$LINE \
#  	      $SUBARGS -l h_stack=64m,h_vmem=2G \
#                 -pe $PE $GATK_THREADS -v GATK_THREADS \
#  	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_nodup_genome" "s_${LINE}_nodup.bam"
#
# 	$SUB -hold_jid $HOLDJID -N cvgG$LINE \
# 	      $SUBARGS -l h_stack=64m,h_vmem=2G \
#               -pe $PE $GATK_THREADS -v GATK_THREADS \
# 	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_genome" "s_${LINE}.bam"
 }

function stat0 {
    echo -e "\033[1mSubmitting stat0 Jobs \033[0m"
    mkdir -p $STAT_DIR
    HOLDJID="recalTableFULL${LINE}"
    export GATK_THREADS=4
    if [ -z $SNPDB ];then
      HOLDJID="dedupNIndexBam${LINE}"
    fi

    $SUB -hold_jid $HOLDJID -N cvgNdG$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G \
               -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_nodup_genome" "s_${LINE}_nodup.bam"

    $SUB -hold_jid $HOLDJID -N cvgG$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G \
              -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_genome" "s_${LINE}.bam"

    if [ ! -z $TARGET ];then
	$SUB -hold_jid $HOLDJID -N cvgNdT$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_nodup_target" "s_${LINE}_nodup.bam" $TARGET

	$SUB -hold_jid $HOLDJID -N cvgT$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_target" "s_${LINE}.bam" $TARGET
    fi
 }

function stat1 {
    echo -e "\033[1mSubmitting stat1 Jobs \033[0m"
    mkdir -p $STAT_DIR
    HOLDJID="recalTableFULL${LINE}"
    export GATK_THREADS=4
    if [ -z $SNPDB ];then
      INFILE="s_${LINE}_nodup_uniq.bam"
      HOLDJID="dedupNIndexBam${LINE}"    
    else
      INFILE="s_${LINE}_nodup_uniq_recal.FULL.bam"
    fi

    $SUB -hold_jid $HOLDJID -N cvgUqG_stat1_$LINE \
	      $SUBARGS -l h_vmem=2.1G \
               -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverageGenome.sh" "$STAT_DIR/s_${LINE}_nodup_uniq_recal_genome" "$INFILE"

    if [ ! -z $TARGET ];then
	$SUB -hold_jid $HOLDJID -N cvgUqT_stat1_$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_nodup_uniq_recal_target" "$INFILE" $TARGET
    fi
    if [ ! -z $FLANK ];then
	$SUB -hold_jid $HOLDJID -N cvgUqF_stat1_$LINE \
	      $SUBARGS -l h_stack=64m,h_vmem=2G -pe $PE $GATK_THREADS -v GATK_THREADS \
	      "$CMD_DIR/depthOfCoverage.sh" "$STAT_DIR/s_${LINE}_nodup_uniq_recal_flank" "$INFILE" $FLANK
    fi
 }

function snpcal {
    echo -e "\033[1mSubmitting Jobs: variant (SNPs, INDELs) -call, -filtration, -evaluation \033[0m"
    mkdir -p $VCF_DIR
    export GATK_THREADS=4

    local JOB_MEM1=$(adjustWorkingMem 256M $GATK_THREADS)
    local JOB_MEM2=$(adjustWorkingMem 10G $GATK_THREADS)

    SUFFIX="_recal.FULL"
    HOLDJID="recalTableFULL${LINE}"

    $SUB -hold_jid "$HOLDJID" -N snpcal${LINE} \
	     $SUBARGS -pe $PE $GATK_THREADS -v GATK_THREADS,TARGET -l h_stack=$JOB_MEM1,h_vmem=$JOB_MEM2 \
	     "$CMD_DIR/snpcal.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}" ${mbq} ${mmq} "s_${LINE}${SUFFIX}"

    $SUB -hold_jid snpcal${LINE} -N snpFiltr$LINE \
	     $SUBARGS -l h_stack=256M,h_vmem=10G \
	     "$CMD_DIR/variantFiltrationNoMask.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}"

    if [ ! -z $SNPDB ];then
      $SUB -hold_jid snpFiltr${LINE} -N snpEval$LINE \
	     $SUBARGS -l h_stack=256M,h_vmem=8G \
	     "$CMD_DIR/variantEval.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}_filtered_nomask"
    fi
}

function snpFiltration {
    echo -e "\033[1mSubmitting variant-filtration Job \033[0m"

    SUFFIX="_recal.FULL"

    $SUB -hold_jid snpcal${LINE} -N snpFiltr$LINE \
	     $SUBARGS -l h_stack=256M,h_vmem=10G \
	     "$CMD_DIR/variantFiltrationNoMask.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}"
}

function snpEvaluation {
    echo -e "\033[1mSubmitting variant-evaluation Job \033[0m"

    SUFFIX="_recal.FULL"

    if [ ! -z $SNPDB ];then
      $SUB -hold_jid snpFiltr${LINE} -N snpEval$LINE \
	     $SUBARGS -l h_stack=256M,h_vmem=8G \
	     "$CMD_DIR/variantEval.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}_filtered_nomask"
    else
    	echo "SNPDB is empty. snpEvaluation cannot start."
    fi
}

function collectSPeekMetrics {
    echo -e "\033[1mSubmitting collectSPeekMetrics Jobs \033[0m"
    $SUB -hold_jid snpEval$LINE,cvgUqT$LINE,cvgUqG$LINE -N SPeekMetrics$LINE \
	     $SUBARGS -v MYSQL_DRAW \
	     "$CMD_DIR/collectSPeekMetrics.sh" $LINE $RGID
}

function insertSPeek {
    echo -e "\033[1mSubmitting insertSPeek Jobs $LINE\033[0m"
    if [[ $DODEBUG ]];then
    	declare -a metric_ids=(68 69 79 80 84 5 6 7 8);
    	declare -a metrics=(10X 20X SNPnum novelSNP ti_tv initialRead duplicate% nonuniq% unmapped%);
    	for i in {0..8};do
    		value=$(grep -P "^0\t\d+\t${metric_ids[$i]}\t" ${LINE}_speek.txt | cut -f4);
    		echo -e "${metrics[$i]}\t$value";
    	done
    else
			$SUB -hold_jid SPeekMetrics$LINE -N insertSPeek$LINE \
				 $SUBARGS -v MYSQL_DRAW \
				 "$CMD_DIR/insertSPeek.sh" $LINE
		fi
}

function snpAnnotation {
    echo -e "\033[1mSubmitting snpAnnotation Jobs \033[0m"

    SUFFIX="_recal.FULL"

    $SUB -hold_jid "snpFiltr${LINE}" -N snpEff${LINE}  \
             $SUBARGS -l h_stack=256m,h_vmem=8G \
             "$CMD_DIR/snpEff.sh" "$VCF_DIR/s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}_filtered_nomask" $SNPEFF
}

function snpcalWG {
    echo -e "\033[1mSubmitting snpcalWG Jobs \033[0m"
    mkdir -p vcf
    export GATK_THREADS=4
    SUFFIX="_recal.FULL"

	$SUB -hold_jid "recalTableFULL${LINE}" -N snpcal${LINE} \
	     $SUBARGS -pe $PE $GATK_THREADS -v GATK_THREADS -l h_vmem=8G \
	     "$CMD_DIR/snpcalWG.sh" ${LINE} $SUFFIX vcf ${mbq} ${mmq}

	$SUB -hold_jid snpcal${LINE} -N snpFiltr$LINE \
	     $SUBARGS -l h_vmem=10G \
	     "$CMD_DIR/variantFiltration.sh" "s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}" vcf "s_${LINE}${SUFFIX}_indel"

	$SUB -hold_jid snpFiltr${LINE} -N snpEval$LINE \
	     $SUBARGS -l h_vmem=8G \
	     "$CMD_DIR/variantEvalWG.sh" "s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}_filtered" vcf
}

function snpcal_celegans {
    echo -e "\033[1mSubmitting snpcal_celegans Jobs \033[0m"
    mkdir -p vcf
    export GATK_THREADS=4
    SUFFIX="_nodup_uniq_recal.FULL"

	$SUB -hold_jid "recalTableFULL${LINE}" -N snpcal${LINE} \
	     $SUBARGS -pe $PE $GATK_THREADS -v GATK_THREADS -l h_vmem=4.5G \
	     "$CMD_DIR/snpcal_celegans.sh" ${LINE} $SUFFIX vcf ${mbq} ${mmq} $TARGET

	$SUB -hold_jid snpcal${LINE},mk_mask$LINE -N snpFiltr$LINE \
	     $SUBARGS -l h_vmem=6.2G \
	     "$CMD_DIR/variantFiltration_celegans.sh" "s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}" vcf "s_${LINE}${SUFFIX}_indel"

# 	$SUB -hold_jid snpFiltr${LINE} -N snpEval$LINE \
# 	     $SUBARGS -l h_vmem=6.2G \
# 	     "$CMD_DIR/variantEval.sh" "s_${LINE}${SUFFIX}_mbq_${mbq}_mmq_${mmq}_filtered" vcf $TARGET
}

function indel_celegans {
    echo -e "\033[1mSubmitting indel_celegans \033[0m"
    SUFFIX="_nodup_uniq_recal.FULL"

	$SUB -hold_jid "recalTableFULL${LINE}" -N indel${LINE} \
	      $SUBARGS -l h_vmem=6.5G \
	      "$CMD_DIR/indel_celegans.sh" ${LINE} "$SUFFIX" vcf

	$SUB -hold_jid indel${LINE} -N mk_mask$LINE \
	      $SUBARGS -l h_vmem=3G \
	      "$CMD_DIR/makeIndelMask.sh" ${LINE} "$SUFFIX"
}
